package com.example.demo.service;

import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.util.IoUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.assertj.core.util.Lists;

import java.io.*;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


/**
 * @Author zhangguangyan
 * @Date 2023-09-09 16:08:46
 */
public class DynamicEasyExcelImportUtils {
    /**
     * 动态获取全部列和数据体，默认从第一行开始解析数据
     *
     * @param stream
     * @return
     */
    public static List<Map<String, String>> parseExcelToView(byte[] stream) {
        return parseExcelToView(stream, 1);
    }

    /**
     * 动态获取全部列和数据体
     *
     * @param stream         excel文件流
     * @param parseRowNumber 指定读取行
     * @return
     */
    public static List<Map<String, String>> parseExcelToView(byte[] stream, Integer parseRowNumber) {
        DynamicEasyExcelListener readListener = new DynamicEasyExcelListener();
        EasyExcelFactory.read(new ByteArrayInputStream(stream)).registerReadListener(readListener).headRowNumber(parseRowNumber).sheet(0).doRead();
        List<Map<Integer, String>> headList = readListener.getHeadList();
        if (CollectionUtils.isEmpty(headList)) {
            throw new RuntimeException("Excel未包含表头");
        }
        List<Map<Integer, String>> dataList = readListener.getDataList();
        if (CollectionUtils.isEmpty(dataList)) {
            throw new RuntimeException("Excel未包含数据");
        }
        //获取头部,取最后一次解析的列头数据
        Map<Integer, String> excelHeadIdxNameMap = headList.get(headList.size() - 1);
        //封装数据体
        List<Map<String, String>> excelDataList = Lists.newArrayList();
        for (Map<Integer, String> dataRow : dataList) {
            Map<String, String> rowData = new LinkedHashMap<>();
            excelHeadIdxNameMap.entrySet().forEach(columnHead -> {
                rowData.put(columnHead.getValue(), dataRow.get(columnHead.getKey()));
            });
            excelDataList.add(rowData);
        }
        return excelDataList;
    }

    /**
     * 文件导入测试
     *
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        //属性项
        FileInputStream inputStream = new FileInputStream(new File("C:\\Users\\92039\\Desktop\\export.xlsx"));
        byte[] stream = IoUtils.toByteArray(inputStream);
        List<Map<String, String>> dataList = parseExcelToView(stream, 1);
        List<String> objects = new ArrayList<>();
        dataList.forEach(d -> {
            objects.addAll(d.keySet());
        });
        System.out.println("属性项:" + objects);
        System.out.println("值:" + dataList);
        inputStream.close();
    }
}
